import asyncio
import ipaddress


async def scan_host_for_ssh(host_ip: str, host_port: int = 22):
    try:
        _, writer = await asyncio.wait_for(
            asyncio.open_connection(host=host_ip, port=host_port), timeout=1
        )
        print(f"IP地址 {host_ip} 正在监听端口 {host_port}")
        writer.close()
    except asyncio.TimeoutError as e:
        pass
    except OSError as e:
        pass


async def scan_network_for_ssh(ip_range: str, host_port: int = 22):
    network = ipaddress.ip_network(ip_range)
    tasks = [
        asyncio.create_task(scan_host_for_ssh(str(host_ip), host_port))
        for host_ip in network.hosts()
    ]
    await asyncio.gather(*tasks)
